package com.huawang.ecolink.apps.controllers.modelica;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.huawang.ecolink.apps.factory.modelica.ModelicaFactory;
import com.huawang.ecolink.apps.response.BaseResponse;
import com.huawang.ecolink.apps.response.ResultEnum;
import com.huawang.ecolink.apps.response.modelica.ModelTreeVO;
import com.huawang.ecolink.apps.service.modelica.ConvertService;
import com.huawang.ecolink.core.languages.modelica.constants.LogMessage;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;


@Api(tags = "转换接口")
@RestController
@RequestMapping("/mlink")
@Slf4j
public class ConvertController {

    @Autowired
    ModelicaFactory modelicaFactory;

    @Autowired
    ConvertService convertService;

    private ObjectMapper objectMapper = new ObjectMapper();

    private static final String PROJECTID = "projectID";
    private static final String INSTANCEID = "instID";

    /*** 
     * @Description: 转换为Modelica后生成映射Xml
     * @param reqBody String
     * @return: com.huawang.ecolink.apps.response.BaseResponse<java.lang.String>
     * @throws:
     */
    @ApiOperation("转换至Modelica")
    @PostMapping("/export/getMapXml")
    @ApiResponses({
            @ApiResponse(code = 1000, message = "OK", response = String.class),
    })
    public BaseResponse<String> getMapXml(@RequestBody String reqBody) throws IOException {
        HashMap<String, String> transReq = objectMapper.readValue(reqBody, HashMap.class);
        String projectId = transReq.get(PROJECTID);
        String instanceId = transReq.get(INSTANCEID);
        return BaseResponse.set(
                ResultEnum.SUCCESS.getCode(),
                ResultEnum.SUCCESS.getTitle(),
                ResultEnum.SUCCESS.getMessage(),
                convertService.getMapXml(projectId, instanceId));
    }

    /**
     * 转换获取节点接口
     *
     * @param projectId  String
     * @param instanceId String
     * @return ModelTreeVO
     */
    @ApiOperation("获取所有模型树")
    @GetMapping("/export/getRootNode")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "projectID", value = "项目id", dataTypeClass = String.class, required = true),
            @ApiImplicitParam(name = "instanceID", value = "元素ID", dataTypeClass = String.class, required = true),
    })
    @ApiResponses({
            @ApiResponse(code = 1000, message = "OK", response = ModelTreeVO.class),
    })

    public BaseResponse<ModelTreeVO> getRootNode(@RequestParam("projectID") String projectId, @RequestParam("instanceID") String instanceId) {
        ModelTreeVO allModelInfo = convertService.getAllModelInfo(projectId, instanceId);
        return BaseResponse.set(
                ResultEnum.SUCCESS.getCode(),
                ResultEnum.SUCCESS.getTitle(),
                ResultEnum.SUCCESS.getMessage(),
                allModelInfo
        );
    }

    /**
     * 转换子节点接口
     *
     * @return
     */
    @ApiOperation("获取子节点")
    @GetMapping("/export/getChildNode")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "projectID", value = "项目id", dataTypeClass = String.class, required = true),
            @ApiImplicitParam(name = "instanceID", value = "元素ID", dataTypeClass = String.class, required = true),
    })
    @ApiResponses({
            @ApiResponse(code = 1000, message = "OK", response = ModelTreeVO.class)
    })
    public BaseResponse<Object> getChildNode(@RequestParam("projectID") String projectId, @RequestParam("instanceID") String instanceId) {
        return BaseResponse.set(
                ResultEnum.SUCCESS.getCode(),
                ResultEnum.SUCCESS.getTitle(),
                ResultEnum.SUCCESS.getMessage(),
                convertService.getChildModelInfo(projectId, instanceId)
        );

    }


    /*** 
     * @Description: 转换为Modelica内容
     * @param reqBody String
     * @return: com.huawang.ecolink.apps.response.BaseResponse<java.lang.String>
     * @throws:
     */
    @ApiOperation("转换至Modelica")
    @PostMapping("/export/modelica")
    @ApiResponses({
            @ApiResponse(code = 1000, message = "OK", response = ModelTreeVO.class)
    })
    public BaseResponse<String> diagramTomodelica(@RequestBody String reqBody) throws IOException {
        HashMap<String, String> transReq = objectMapper.readValue(reqBody, HashMap.class);
        String projectId = transReq.get(PROJECTID);
        String instanceId = transReq.get(INSTANCEID);
        return BaseResponse.set(
                ResultEnum.SUCCESS.getCode(),
                ResultEnum.SUCCESS.getTitle(),
                ResultEnum.SUCCESS.getMessage(),
                convertService.transToModelica(projectId, instanceId));
    }

    /**
     * 在对应路径保存生成的.mo文件
     *
     * @return
     */
    @ApiOperation("保存文件")
    @PostMapping("/export/saveMoFile")
    @ApiResponses({
            @ApiResponse(code = 1000, message = "OK", response = Void.class)
    })
    public BaseResponse<String> saveMoFile(@RequestBody String reqBody) throws IOException {
        Map<String, String> transReq = objectMapper.readValue(reqBody, HashMap.class);
        String projectId = transReq.get(PROJECTID);
        String instanceId = transReq.get("instanceId");
        String dirPath = transReq.get("dirPath");
        convertService.saveMoFile(projectId, instanceId, dirPath);
        return BaseResponse.success();
    }


    /*** 
     * @Description: 同步刷新接口，功能为删除缓存，重新加载结构树
     * @param projectId String
     * @return: com.huawang.ecolink.apps.response.BaseResponse<java.lang.Object>
     * @throws:
     */
    @ApiOperation("同步刷新接口，删除缓存，重新加载模型树")
    @GetMapping("/export/synModelTree")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "projectID", value = "项目id", required = true)
    })
    @ApiResponses({
            @ApiResponse(code = 1000, message = "OK")
    })
    public BaseResponse<Object> synModelTree(@RequestParam("projectID") String projectId) {
        convertService.synModelTree(projectId);
        return BaseResponse.set(
                ResultEnum.SUCCESS.getCode(),
                ResultEnum.SUCCESS.getTitle(),
                ResultEnum.SUCCESS.getMessage(),
                null
        );

    }

    /**
     * 弹窗转换-转换日志接口
     *
     * @return
     */
    @ApiOperation("获取转换日志")
    @PostMapping("/export/getTransLog")
    @ApiResponses({
            @ApiResponse(code = 1000, message = "OK", response = List.class)
    })
    public BaseResponse<List<LogMessage>> getTransLog(@RequestBody String reqBody) throws IOException {
        HashMap<String, String> transReq = objectMapper.readValue(reqBody, HashMap.class);
        String projectId = transReq.get(PROJECTID);
        String instanceId = transReq.get(INSTANCEID);
        return BaseResponse.set(
                ResultEnum.SUCCESS.getCode(),
                ResultEnum.SUCCESS.getTitle(),
                ResultEnum.SUCCESS.getMessage(),
                convertService.getTransLog(projectId, instanceId)
        );
    }

    /*** 
     * @Description: 模型复用，通过更新更改的模型文件。
     * @param reqBody String
     * @return: com.huawang.ecolink.apps.response.BaseResponse<java.lang.String>
     * @throws:
     */
    @ApiOperation("同步模型")
    @PostMapping("/export/synElement")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "projectID", value = "项目id", required = true),
            @ApiImplicitParam(name = "instanceID", value = "元素ID", required = true),
            @ApiImplicitParam(name = "dirPath", value = "导出的路径", required = true),
    })
    @ApiResponses({
            @ApiResponse(code = 1000, message = "OK", response = List.class)
    })
    public BaseResponse<String> synElement(@RequestBody String reqBody) throws IOException {
        Map<String, String> transReq = objectMapper.readValue(reqBody, HashMap.class);
        String projectId = transReq.get(PROJECTID);
        String instanceId = transReq.get("instanceID");
        String dirPath = transReq.get("dirPath");
        convertService.saveMoFile(projectId, instanceId, dirPath);
        return BaseResponse.success("");
    }

    /***
     * @Description: 根据名称模糊查询元素, 返回值data为id数组
     * @param projectId String
     * @param instanceId String
     * @param searchName String
     * @return: com.huawang.ecolink.apps.response.BaseResponse<java.util.List < com.huawang.ecolink.apps.response.modelica.ModelTreeVO>>
     * @throws:
     */
    @ApiOperation("根据名称模糊查询元素,返回值data为id数组")
    @GetMapping("/search/likebyname")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "projectID", value = "项目id", required = true),
            @ApiImplicitParam(name = "instanceID", value = "元素ID", required = true),
            @ApiImplicitParam(name = "searchName", value = "输入模糊搜索", required = true),
    })
    @ApiResponses({
            @ApiResponse(code = 1000, message = "OK", response = List.class),
    })
    public BaseResponse<List<ModelTreeVO>> searchByName(@RequestParam("projectID") String projectId, @RequestParam("instanceID") String instanceId,
                                                        @RequestParam("searchName") String searchName) {

        return BaseResponse.success(convertService.searchByName(projectId, instanceId, searchName));

    }


}
